package com.jinyou.utils.common;

public class AngleUtils {

    /**
     * @param latA 纬度1
     * @param lngA 经度1
     * @param latB 纬度2
     * @param lngB 经度2
     * @return
     */
    private static double getAngle1(double latA, double lngA, double latB, double lngB) {
        double y = Math.sin(lngB - lngA) * Math.cos(latB);
        double x = Math.cos(latA) * Math.sin(latB) - Math.sin(latA) * Math.cos(latB) * Math.cos(lngB - lngA);
        double brng = Math.atan2(y, x);

        brng = Math.toDegrees(brng);
        if (brng < 0)
            brng = brng + 360;
        return brng;
    }

    /**
     * 获取相差方位角
     *
     * @param deliveryLat 骑手纬度
     * @param deliveryLng 骑手经度
     * @param orderLat    身上订单用户纬度
     * @param orderLng    身上订单用户经度
     * @param newLat      新来订单用户纬度
     * @param newLng      新来订单用户经度
     * @return
     */
    public static double getSubAngle(double deliveryLat, double deliveryLng, double orderLat, double orderLng, double newLat, double newLng) {
        Double aAngle = getAngle1(deliveryLat, deliveryLng, orderLat, orderLng);
        Double bAngle = getAngle1(deliveryLat, deliveryLng, newLat, newLng);
        if (aAngle >= 90 && aAngle < 180) {
            aAngle = aAngle - 90;
        } else if (aAngle >= 180 && aAngle < 270) {
            aAngle = aAngle - 180;
        } else if (aAngle >= 270 && aAngle < 360) {
            aAngle = aAngle - 270;
        }
        Double subAngle;
        if (bAngle <= 90 && bAngle <= aAngle) {
            subAngle = aAngle - bAngle;
        } else if (bAngle > 270) {
            subAngle = aAngle - (360 - bAngle);
        } else {
            subAngle = bAngle - aAngle;
        }
//        subAngle = aAngle + bAngle;
        return subAngle;
    }


    //    public static double getSubAngle(double deliveryLat, double deliveryLng, double orderLat, double orderLng, double newLat, double newLng) {
//        Double aAngle = getAngle1(deliveryLat, deliveryLng, orderLat, orderLng);
//        Double bAngle = getAngle1(deliveryLat, deliveryLng, newLat, newLng);
//        if (aAngle >= 90 && aAngle < 180) {
//            aAngle = aAngle - 90;
//        } else if (aAngle >= 180 && aAngle < 270) {
//            aAngle = aAngle - 180;
//        } else if (aAngle >= 270 && aAngle < 360) {
//            aAngle = aAngle - 270;
//        }
//        Double subAngle;
//        if (bAngle <= 90 && bAngle <= aAngle) {
//            subAngle = aAngle - bAngle;
//        } else if (bAngle > 270) {
//            subAngle = aAngle - (360 - bAngle);
//        } else {
//            subAngle = bAngle - aAngle;
//        }
////        subAngle = aAngle + bAngle;
//        return subAngle;
//    }

    public static int getDegree(double vertexPointX, double vertexPointY, double point0X, double point0Y, double point1X, double point1Y) {
        //向量的点乘
        double vector = (point0X - vertexPointX) * (point1X - vertexPointX) + (point0Y - vertexPointY) * (point1Y - vertexPointY);
        //向量的模乘
        double sqrt = Math.sqrt(
                (Math.abs((point0X - vertexPointX) * (point0X - vertexPointX)) + Math.abs((point0Y - vertexPointY) * (point0Y - vertexPointY)))
                        * (Math.abs((point1X - vertexPointX) * (point1X - vertexPointX)) + Math.abs((point1Y - vertexPointY) * (point1Y - vertexPointY)))
        );
        //反余弦计算弧度
        double radian = Math.acos(vector / sqrt);

        //弧度转角度制
        return (int) (180 * radian / Math.PI);
    }



    private static int getTest(double x1,double y1,double x2,double y2,double x3,double y3){
        double radius = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        double atan = Math.atan2((y2-y1),(x2-x1));
        System.out.println(radius + ":" +atan);
        return 0;
    }


    public static void main(String[] args) {
        System.out.println(getSubAngle(22.536911,113.943185,22.538667,113.944173,22.539211,113.950961));

        System.out.println(getDegree(113.943185,22.536911,113.950961,22.539211,113.944173,22.538667));
    }

}
